var zoqui;
if (typeof(zoqui) == "undefined")    zoqui = {};

if (typeof(zoqui.Ajax) == "undefined")
	zoqui.Ajax = {}

zoqui.Ajax = function ()
{
	this.req = {};
	this.isIE = false;
}

zoqui.Ajax.prototype.makeRequest = function(url, onComplete, method, data)
{
	this.onComplete = onComplete;
	var pointer = this;		
	if (window.XMLHttpRequest){
		// native XMLHttpRequest object
		this.req = new XMLHttpRequest();
		this.req.onreadystatechange = function () { pointer.processReqChange() };
        //this.req.overrideMimeType('text/html; charset=utf-8');
		//method="GET";
		//data = "format=json";
		//url = "http://xpenser.com/api/v1.0/expenses/?format=json";		
		//var auth = this.base64('mazoqui@gmail.com','mu59fu');
		//this.req.setRequestHeader('Authorization', auth);
		//this.req.setRequestHeader('Host','xpenser.com');
		//this.req.setRequestHeader('Accept','*/*');
		if (method=='GET'){
			this.req.open(method, url, true); //
			this.req.send(null);
		}
		else{
			data="q=xx+10+yy";
			this.req.open(method, url, true,'mazoqui@gmail.com','mu59fu');
			this.req.setRequestHeader('Authorization', "Basic "+(Base64.encode('mazoqui@gmail.com'+':'+'mu59fu')));
			this.req.withCredentials = true;
			this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.req.setRequestHeader("Content-Length",data.length);
			this.req.setRequestHeader("Connection", "close");
			this.req.send(data);
		}
	}
	else if (window.ActiveXObject){
		// IE/Windows ActiveX version
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req){
			this.req.onreadystatechange = function () { pointer.processReqChange() };
			this.req.open(method, url, true);
			this.req.send();
		}
	}
};

zoqui.Ajax.prototype.processReqChange = function()
{
	// only if req shows "loaded"
	if (this.req.readyState == 4) {
		var headers = this.req.getAllResponseHeaders();
		if (this.req.status == 200){
			this.onComplete( {status:'OK',data: this.req} );
		} else {
            this.onComplete( {status:'ERROR',data: null} );
		}
	}
}


zoqui.Ajax.prototype.get = function(url, onComplete)
{
	this.makeRequest(url,onComplete,"GET");
}

zoqui.Ajax.prototype.post = function(url, onComplete, data)
{
	this.makeRequest(url,onComplete,"POST", this.formEncode(data));
}

zoqui.Ajax.prototype.getParameterList = function(parameters) 
{
	// copied from oauth
	if (parameters == null) {
		return [];
	}
	if (typeof parameters != "object") {
		return this.decodeForm(parameters + "");
	}
	if (parameters instanceof Array) {
		return parameters;
	}
	var list = [];
	for (var p in parameters) {
		list.push([p, parameters[p]]);
	}
	return list;
}

zoqui.Ajax.prototype.formEncode = function(parameters)
{
	// copied from oauth
	var form = "";
	var list = this.getParameterList(parameters);
	for (var p = 0; p < list.length; ++p) {
		var value = list[p][1];
		if (value == null) value = "";
		if (form != "") form += '&';
		form += this.percentEncode(list[p][0])
		  +'='+ this.percentEncode(value);
	}
	return form;
}

zoqui.Ajax.prototype.percentEncode = function(s) 
{
	// copied from oauth
	if (s == null) {
		return "";
	}
	if (s instanceof Array) {
		var e = "";
		for (var i = 0; i < s.length; ++s) {
			if (e != "") e += '&';
			e += this.percentEncode(s[i]);
		}
		return e;
	}
	s = encodeURIComponent(s);
	// Now replace the values which encodeURIComponent doesn't do
	// encodeURIComponent ignores: - _ . ! ~ * ' ( )
	// OAuth dictates the only ones you can ignore are: - _ . ~
	// Source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent
	s = s.replace(/\!/g, "%21");
	s = s.replace(/\*/g, "%2A");
	s = s.replace(/\'/g, "%27");
	s = s.replace(/\(/g, "%28");
	s = s.replace(/\)/g, "%29");
	return s;
}

String.prototype.trim = function(){
   return this.replace(/[\s]{1,}/g," ").replace(/^\s*|\s*$/g,"");
};	

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/ 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

}